package com.diancan.modules.good;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diancan.common.Result;
import com.diancan.domain.Good;
import com.diancan.domain.Tabs;
import com.diancan.modules.good.tabs.TabsService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
@RequestMapping("good")
@CrossOrigin
public class GoodController {
    private final GoodService goodService;
    private final TabsService tabsService;

    public GoodController(GoodService goodService, TabsService tabsService) {
        this.goodService = goodService;
        this.tabsService = tabsService;
    }

    /**
     * 获取分类
     *
     * @return
     */
    @GetMapping("selectTabs")
    public Result selectTabs() {
        QueryWrapper<Tabs> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort").orderByAsc("id");
        ArrayList<Tabs> arrayList = (ArrayList<Tabs>) tabsService.list(wrapper);
        ArrayList<Tabs> returnArrayList = new ArrayList<>();
        Tabs tabs = new Tabs();
        tabs.setId(0);
        tabs.setName("全部");
        returnArrayList.add(tabs);
        for (Tabs a : arrayList) {
            returnArrayList.add(a);
        }
        return Result.buildSuccessResult(returnArrayList);
    }

    /**
     * 获取菜单
     *
     * @return
     */
    @GetMapping("selectAllgoods")
    public Result selectAllgoods() {
        QueryWrapper<Good> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id").eq("putaway",1);
        ArrayList<Good> goodArrayList = (ArrayList<Good>) goodService.list(wrapper);
        ArrayList<Good> returnList = (ArrayList<Good>) goodService.list(wrapper);

        for (Good g : goodArrayList) {
            g.setTabId(0);
            returnList.add(g);
        }

        ArrayList<ArrayList<Good>> r = new ArrayList<>();
        r.add(returnList);
        r.add((ArrayList<Good>) goodService.list(wrapper));

        return Result.buildSuccessResult(r);
    }
}
